package com.tedu.weibo.interceptor;

import com.tedu.weibo.pojo.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("zol");
       /*
       HttpServletRequest请求对象中提供了一个方法
       getSession()，用于获取当前用户的session对象，如果当前用户没有session，则创建一个session对象，并返回
        */
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null){
            //设置响应对象，要求浏览器请求"/index.html"页面内容
            response.sendRedirect("/index.html");
            return false;
        }else {

            return true;
        }

    }
}
